package com.lyf.shop.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.lyf.common.core.utils.StringUtils;
import com.lyf.common.security.utils.SecurityUtils;
import com.lyf.shop.domain.Classification;
import com.lyf.shop.dto.RegionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lyf.common.log.annotation.Log;
import com.lyf.common.log.enums.BusinessType;
import com.lyf.common.security.annotation.RequiresPermissions;
import com.lyf.shop.domain.Region;
import com.lyf.shop.service.IRegionService;
import com.lyf.common.core.web.controller.BaseController;
import com.lyf.common.core.web.domain.AjaxResult;
import com.lyf.common.core.utils.poi.ExcelUtil;
import com.lyf.common.core.web.page.TableDataInfo;

/**
 * 商品所属区域Controller
 * 
 * @author lyf
 * @date 2023-02-10
 */
@RestController
@RequestMapping("/region")
public class RegionController extends BaseController
{
    @Autowired
    private IRegionService regionService;

    /**
     * 查询商品所属区域列表
     */
    @RequiresPermissions("supermarket:region:list")
    @GetMapping("/list")
    public TableDataInfo list(RegionDto regionDto)
    {
        startPage();
        List<RegionDto> list = regionService.selectRegionNameList(regionDto);
        return getDataTable(list);
    }

    /**
     * 导出商品所属区域列表
     */
    @RequiresPermissions("supermarket:region:export")
    @Log(title = "商品所属区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Region region)
    {
        List<Region> list = regionService.selectRegionList(region);
        ExcelUtil<Region> util = new ExcelUtil<Region>(Region.class);
        util.exportExcel(response, list, "商品所属区域数据");
    }

    /**
     * 获取商品所属区域详细信息
     */
    @RequiresPermissions("supermarket:region:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(regionService.selectRegionById(id));
    }

    /**
     * 新增商品所属区域
     */
    @RequiresPermissions("supermarket:region:add")
    @Log(title = "商品所属区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Region region)
    {
        return toAjax(regionService.insertRegion(region));
    }

    /**
     * 修改商品所属区域
     */
    @RequiresPermissions("supermarket:region:edit")
    @Log(title = "商品所属区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Region region)
    {
        return toAjax(regionService.updateRegion(region));
    }

    /**
     * 删除商品所属区域
     */
    @RequiresPermissions("supermarket:region:remove")
    @Log(title = "商品所属区域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(regionService.deleteRegionByIds(ids));
    }


    /**
     * 状态修改
     */
    @RequiresPermissions("supermarket:region:edit")
    @Log(title = "商品区域状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Region region)
    {
        region.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(regionService.updateRegion(region));
    }

    @RequiresPermissions("supermarket:region:query")
    @GetMapping(value = "/regionName/{userId}")
    public AjaxResult getClassificationNameByUserId(@PathVariable("userId") Long userId){
        if (StringUtils.isNotNull(userId)){
            List<Region> regionList = regionService.getRegionNameByUserId(userId);
            return AjaxResult.success(regionList);
        }
        return AjaxResult.error("用户id不能为空");
    }
}
